package com.xhs.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mybatisflex.core.query.If;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xhs.entity.TagsEntity;
import com.xhs.mapper.TagsMapper;
import com.xhs.message.ReturnResult;
import com.xhs.service.TagsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.xhs.entity.table.TagsEntityTableDef.TAGS_ENTITY;

/**
 * @desc:
 * @projectName: spring-boot-demo
 * @author: xhs
 * @date: 2024-12-16 周一 10:01
 * @version: JDK 1.8
 */
@Slf4j
@Service
public class TagsServiceImpl extends ServiceImpl<TagsMapper, TagsEntity> implements TagsService {
    /**
     * 根据标签查询
     *
     * @param name 名称
     * @param tags 标签
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> getTags(String name, List<String> tags) {
//        queryChain()
//                .select(TAGS_ENTITY.ALL_COLUMNS)
//                .from(TAGS_ENTITY)
//                .where(TAGS_ENTITY.NAME.like(name, If::hasText))
//                .and(TAGS_ENTITY.TAGS.like())

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(TAGS_ENTITY.ID,TAGS_ENTITY.TAGS);
        queryWrapper.from(TAGS_ENTITY);
        queryWrapper.where(TAGS_ENTITY.NAME.like(name, If::hasText));
        if (!CollUtil.isEmpty(tags)) {
            for (String tag : tags) {
                queryWrapper.and(TAGS_ENTITY.TAGS.like("\"" + tag + "\""));

            }
        }
        List<TagsEntity> list = this.list(queryWrapper);
        log.info("list:{}", list);
        return ReturnResult.success(list);
    }

}
